<?php

// 登录
// POST member.login

namespace app\api\resource\member;
use think\Db;
use think\Loader;
// use think\Session;
// use think\Cookie;
use app\common\logic\Login as LoginLogic;

class Login
{
    // 方法
    public $_method = ['POST'];
    // 前置
    public $_pre    = [];
    // 描述
    public $_description = '登录';
    // 参数
    public $_param  = [
        'mobile'   => '手机号',
        'password' => '密码',
    ];
    // 返回
    public $_return = [
        201 => '登录成功',
        400 => '帐号不存在或密码错误',
        422 => '请求参数不合法',
        423 => '帐号被锁定',
    ];

    public function run(&$request)
    {
        $params['mobile']   = $request->post('mobile/s','','trim');
        $params['password'] = $request->post('password/s','','trim');

        $validate = Loader::validate('Member');
        if ( !$validate->scene('login')->check($params) ) {
            $errMsg = $validate->getError();
            return [422,$errMsg];
        }

        $where['mobile']   = $params['mobile'];
        $where['password'] = md5($params['password']);
        $member = Db::name('member')->where($where)->field('password',true)->find();

        if ( is_null($member) )     return [400,'帐号不存在或密码错误'];
        if (1 == $member['status']) return [423,'帐号被锁定'];

		$member['info'] = Db::name('member_info')->where('member_id',$member['id'])->find();

        // 登录处理
        LoginLogic::run($member);
        
        // 写入Session
        // Session::set('member',$member);
        
        // 写入Cookie
        // Cookie::set('memberId',$member['id'],0);
        // Cookie::set('memberRole',$member['role'],0);
        // Cookie::set('memberCert',!$member['status'],0);

        return [ 201,[] ];
    }
}